package com.justdoit.facedemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.justdoit.facedemo.base.Result;
import com.justdoit.facedemo.base.Results;
import com.justdoit.facedemo.domain.Meeting;
import com.justdoit.facedemo.domain.PunchCard;
import com.justdoit.facedemo.domain.PunchCardVo;
import com.justdoit.facedemo.mapper.PunchCardMapper;
import com.justdoit.facedemo.service.PunchCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/punchCad")
public class PunchCardController {
    @Autowired
    private PunchCardMapper punchCardMapper;

    @Autowired
    private PunchCardService punchCardService;

    //条件查询补签的人然后分页
    @GetMapping("/findPage")
    public Map<String,Object> findPage2(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        //如果前端没有传值就默认为空
                                        @RequestParam(defaultValue = "") String user_name,
                                        @RequestParam(defaultValue = "") String state){

        //分页计算
        pageNum = (pageNum-1)*pageSize;
        //模糊查询拼接
        if (!"null".equals(user_name)){
            user_name = "%"+user_name+"%";
        }
        if (!"null".equals(state)){
            state = "%"+state+"%";
        }
        //查询到的所有补签的人
        List<PunchCardVo> punchCardVos = punchCardMapper.selectCard(user_name, state, pageNum, pageSize);
        //查询到的补签总人数
        Integer total = punchCardMapper.selectTotal(user_name, state);
        HashMap<String, Object> res = new HashMap<>();
        res.put("data",punchCardVos);
        res.put("total",total);
        return res;
    }
    //会议的补签申请
    @PostMapping("/saveCard")
    public boolean addPunchCad(@RequestBody PunchCard punchCard){
        System.out.println(punchCard);
        return punchCardService.save(punchCard);
    }
    //会议的补签申请处理
    @PostMapping("/UpdateCardState")
    //修改用户对应的签到卡为补签
    public boolean MyUpdate(@RequestBody PunchCard punchCard){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        //需要修改签到卡的用户Id
        updateWrapper.eq("user_id", punchCard.getUserId());
        //需要修改签到卡对应的会议Id
        updateWrapper.eq("meeting_id", punchCard.getMeetingId());
        //被修改的签到卡Id
        updateWrapper.eq("punch_id", punchCard.getPunchId());
        //需要修改的字段
        updateWrapper.set("state", "已签到");
        //备注为补签
        updateWrapper.set("remark","补签");
        return punchCardService.update(null,updateWrapper);
    }
    @PostMapping("/UpdateCardState2")
    //修改用户对应的签到卡为取消补签
    public boolean MyUpdate2(@RequestBody PunchCard punchCard){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        //需要修改签到卡的用户Id
        updateWrapper.eq("user_id", punchCard.getUserId());
        //需要修改签到卡对应的会议Id
        updateWrapper.eq("meeting_id",punchCard.getMeetingId());
        //被修改的签到卡Id
        updateWrapper.eq("punch_id",punchCard.getPunchId());
        //修改状态为为签到
        updateWrapper.set("state","未签到");
        //修改备注为取消补签的理由
        updateWrapper.set("`remark`",punchCard.getRemark());
        return punchCardService.update(null,updateWrapper);
    }

    /**
     * 签到请求
     *
     * @param map 需要传递的参数
     *            meeting_id：会议id
     *            user_id：用户id
     * @return 签到成功或失败描述
     */
    @PostMapping("/signIn")
    public Result<Object> SignIn(@RequestBody Map<String, String> map) {
        // 会议id
        String meeting_id = map.get("meeting_id");

        // 用户id
        String user_id = map.get("user_id");

        // 格式化日期，签到时间，就是当前时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String nowTime = format.format(date);

        // 判断是否已经签到过了
        QueryWrapper<PunchCard> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user_id).eq("meeting_id", meeting_id);
        PunchCard punchCard = punchCardMapper.selectOne(wrapper);

        // 我这里会判断是否已经签到ok
        if (punchCard != null) return Results.newError("您已签到，无需再次签到");

        boolean code = punchCardService.save(
                new PunchCard(null,Integer.parseInt(user_id), Integer.parseInt(meeting_id)
                        , "已签到", nowTime, "人脸签到", "正常签到"));
        return code ? Results.newFailedResult("200", "签到成功") : Results.newError("签到失败，请稍后重试");

    }

}
